﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式.观察者模式
{
    public class Subject
    {
        private List<Observer> observers = new List<Observer>();

        private int state;

        public int State
        {
            set
            {
                state = value;
                NotifyAllObservers();
            }
            get
            {
                return state;
            }
        }

        public void Attach(Observer observer)
        {
            observers.Add(observer);
        }

        public void NotifyAllObservers()
        {
            foreach (Observer item in observers)
            {
                item.Update();
            }
        }
    }
}
